import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/api/service/user_service.dart';
import 'package:hm_show_flutter/config/app_build.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/register_entity.dart';
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/toast_util.dart';
import 'package:hm_show_flutter/widgets/support_view.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  ///是否隐藏密码
  bool isObscure = true;

  TextEditingController _accountTextControl = TextEditingController();
  TextEditingController _passwordTextControl = TextEditingController();
  TextEditingController _passwordTextControl2 = TextEditingController();
  UserService userService = UserService();
  RegisterEntity userEntity;

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    final logo = Container(
      width: 80,
      height: 80,
      child: ClipRRect(
        //圆角图片
        borderRadius: BorderRadius.circular(6),
        child: Image.asset(AppBuild.app_ic_ion),
      ),
    );

    final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      inputFormatters: [
        WhitelistingTextInputFormatter(RegExp("[a-z,A-Z,0-9]")), //限制只允许输入字母和数字
        LengthLimitingTextInputFormatter(11), //限制输入长度不超过8位
      ],
      controller: _accountTextControl,
      decoration: InputDecoration(
        hintText: Strings.LOGIN_HINT_ACCOUNT,
        hintStyle: TextStyle(fontSize: 12),
        prefixIcon: Icon(
          Icons.account_circle,
          size: 18,
        ),
        contentPadding: EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.mainColor)),
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.grey)),
      ),
    );

    final password = TextFormField(
      autofocus: false,
      obscureText: isObscure,
      inputFormatters: [
        WhitelistingTextInputFormatter(RegExp("[a-z,A-Z,0-9]")), //限制只允许输入字母和数字
        LengthLimitingTextInputFormatter(11), //限制输入长度不超过8位
      ],
      controller: _passwordTextControl,
      decoration: InputDecoration(
        hintText: Strings.LOGIN_HINT_PASSWORD,
        hintStyle: TextStyle(
          fontSize: 12,
        ),
        prefixIcon: Icon(
          Icons.lock,
          size: 18,
        ),
        suffixIcon: GestureDetector(
          child: Icon(
            isObscure ? Icons.visibility : Icons.visibility_off,
            size: 18,
          ),
          onTap: () {
            setState(() {
              isObscure = !isObscure;
            });
          },
        ),
        contentPadding: EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.mainColor)),
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.grey)),
      ),
    );
    final password2 = TextFormField(
      autofocus: false,
      obscureText: isObscure,
      inputFormatters: [
        WhitelistingTextInputFormatter(RegExp("[a-z,A-Z,0-9]")), //限制只允许输入字母和数字
        LengthLimitingTextInputFormatter(11), //限制输入长度不超过8位
      ],
      controller: _passwordTextControl2,
      decoration: InputDecoration(
        hintText: Strings.LOGIN_HINT_PASSWORD1,
        hintStyle: TextStyle(
          fontSize: 12,
        ),
        prefixIcon: Icon(
          Icons.lock,
          size: 18,
        ),
        suffixIcon: GestureDetector(
          child: Icon(
            isObscure ? Icons.visibility : Icons.visibility_off,
            size: 18,
          ),
          onTap: () {
            setState(() {
              isObscure = !isObscure;
            });
          },
        ),
        contentPadding: EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.mainColor)),
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32.0),
            borderSide: BorderSide(width: 1, color: AppColors.grey)),
      ),
    );

    final loginButton = Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: 24, right: 24),
      child: RaisedButton(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(24),
        ),
        onPressed: _register,
        padding: EdgeInsets.all(13),
        color: AppColors.mainColor,
        child: Text(Strings.REGISTER,
            style: TextStyle(
              color: Colors.white,
            )),
      ),
    );

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: PreferredSize(
          child: AppBar(
            iconTheme: IconThemeData(color: Colors.white),
            automaticallyImplyLeading: true,
            centerTitle: true,
            title: Text(
              "注册账号",
              style: TextStyle(color: Colors.white, fontSize: 16.0),
            ),
          ),
          preferredSize: Size.fromHeight(ScreenUtil.instance.height * 0.036)),
      body: Center(
        child: Column(
          children: <Widget>[
            SizedBox(height: 20.0),
            logo,
            SizedBox(height: 28.0),
            Padding(
              padding: EdgeInsets.only(left: 24, right: 24),
              child: email,
            ),
            SizedBox(height: 24.0),
            Padding(
              padding: EdgeInsets.only(left: 24, right: 24),
              child: password,
            ),
            SizedBox(height: 6.0),
            Padding(
              padding: EdgeInsets.only(left: 24, right: 24),
              child: password2,
            ),
            SizedBox(height: 35.0),
            loginButton,
            Expanded(child: Container()),
            SupportView()
          ],
        ),
      ),
      resizeToAvoidBottomPadding: false,
    );
  }

  _register() {
    String _account = _accountTextControl.text.toString();
    String _password = _passwordTextControl.text.toString();
    String _password2 = _passwordTextControl2.text.toString();

    if (checkRegister(_account, _password, _password2)) {
      userService.register(_account, _password, (success) {
        userEntity = success;
        ToastUtil.showToast(Strings.REGISTER_SUCCESS);
        Navigator.pop(context);
      }, (onFail) {
        if (onFail != null) {
          print(onFail);
          ToastUtil.showToast(onFail);
        }
      });
    }
  }

  bool checkRegister(String account, String pwd1, String pwd2) {
    if (account == null || account.isEmpty) {
      ToastUtil.showToast("请输入账号");
      return false;
    }
    if (pwd1 == null || pwd2 == null || pwd1.isEmpty || pwd2.isEmpty) {
      ToastUtil.showToast("请输入密码");
      return false;
    }

    if (pwd1 != pwd2) {
      ToastUtil.showToast("两次密码不一致");
      return false;
    }
    return true;
  }
}
